//index.js
//获取应用实例
const app = getApp()
let that;
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  

  onTap: (event) => {
    //父子跳转
    // wx.navigateTo({
    //   url: '../posts/posts',
    //   success: (res)=>{
    //     console.log('跳转成功');
    //   },
    //   fail:()=>{
    //     console.log('跳转失败');
    //   },
    //   complete:()=>{
    //     console.log("跳转完成");
    //   }
    // });
    //平行跳转
    wx.checkIsSupportSoterAuthentication({
      success:(res)=> {
        console.log(res);
          // res.supportMode = [] 不具备任何被SOTER支持的生物识别方式
          // res.supportMode = ['fingerPrint'] 只支持指纹识别
          // res.supportMode = ['fingerPrint', 'facial'] 支持指纹识别和人脸识别
      },
      fail:(res)=>{
        console.log(res);
      }
  });

  wx.startSoterAuthentication({
    requestAuthModes: ['fingerPrint'],
    challenge: '123456',
    authContent: '请用指纹解锁',
    success(res) {
      wx.switchTab({
        url: '../posts/posts',
        success: (res) => {
          console.log('跳转成功');
        },
        fail: () => {
          console.log('跳转失败');
        },
        complete: () => {
          console.log("跳转完成");
        }
      });
    },
    fail(res){
      wx.switchTab({
        url: '../posts/posts',
        success: (res) => {
          console.log('跳转成功');
        },
        fail: () => {
          console.log('跳转失败');
        },
        complete: () => {
          console.log("跳转完成");
        }
      });
    }
  })
  },

//平行跳转后直接执行unload
  onUnload: () => {
    console.log("unload");
  },

//父子跳转后直接执行onhide
  onHide: () => {
    console.log("onhide");
  },

  onLoad: function () {
    that = this;

    wx.showShareMenu({
      withShareTicket: true
    })

    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (that.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      wx.login({
        success: function(res){
          wx.getUserInfo({
            success: res => {
              app.globalData.userInfo = res.userInfo;
              console.log(res);
              that.setData({
                userInfo: res.userInfo,
                hasUserInfo: true
              })
            }
          })
        },
        fail: function() {
         console.log(res);
        },     
      })
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },



  onShareAppMessage: function (res) {
    console.log(res);
    if(res.from==="button"){
      return {
        title: '分享',
        path: '/pages/index/index',
        imageUrl:'/images/post/crab.png',
        success: function(res) {
          console.log('转发成功'+res);
        },
        fail: function(res) {
          console.log('转发失败');
        }
      }
    }
    return {
      title: '分享',
      path: '/pages/posts/posts',
      success: function(res) {
        console.log('转发成功');
        console.log(res);
      },
      fail: function(res) {
        console.log('转发失败');
      }
    }
  },
})
